<template>
    <a-card :bordered="false" v-if="!show">
        <module-search ref="search" @search="handleSearch" @add="handleAdd"></module-search>
        <module-table ref="table" @update="handleUpdate" />
    </a-card>
    <form-generator ref="form" v-else :id="id" @save="handleSave"></form-generator>
</template>

<script>
import ModuleSearch from './search';
import ModuleTable from './table';
import FormGenerator from '@/components/Form/AntdGenerator';

export default {
    name: 'AutoForm',
    components: {
        ModuleTable,
        ModuleSearch,
        FormGenerator
    },
    provide() {
        return {
            backList: function() {
                this.show = false;
            }.bind(this)
        };
    },
    data() {
        return {
            show: false,
            id: null
        };
    },
    methods: {
        handleSearch(query) {
            this.$refs.table.handleLoadData(query);
        },
        handleAdd() {
            this.id = null;
            this.show = true;
        },
        handleUpdate(row) {
            this.id = row.dataFormId;
            this.show = true;
        },
        handleSave() {
            this.show = false;
            this.$nextTick(() => {
                this.$refs.table.handleLoadData();
            });
        }
    }
};
</script>
